package com.jack.reactor;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Created by zhang_j on 2019/10/23
 */
public class MessageTest {
    public static void main(String[] args) {
        errorMessageTest();
        errorMessageDefaultTest();
        onErrorResumeTest();
        retryTest();
    }

    public static void retryTest(){
        Flux.just(1,2)
                .concatWith(Mono.error(new IllegalStateException()))
                .retry(1)
                .subscribe(System.out::println);
    }

    public static void onErrorResumeTest(){
        Flux.just(1, 2)
                .concatWith(Mono.error(new IllegalStateException()))
                .onErrorResume(e -> {
                    if (e instanceof IllegalStateException) {
                        return Mono.just(0);
                    } else if (e instanceof IllegalArgumentException) {
                        return Mono.just(-1);
                    }
                    return Mono.empty();
                })
                .subscribe(System.out::println);
    }

    public static void errorMessageDefaultTest(){
        Flux.just(1, 2)
                .concatWith(Mono.error(new IllegalStateException()))
                .onErrorReturn(0)
                .subscribe(System.out::println);
    }

    public static void errorMessageTest(){
        Flux.just(1, 2)
                .concatWith(Mono.error(new IllegalStateException()))
                .subscribe(System.out::println, System.err::println);
    }
}
